from django.db import models
from libs import ModelMixin, human_datetime
from apps.account.models import User


class Register(models.Model, ModelMixin):
    name = models.CharField("注册中心名称", max_length=50, unique=True, help_text="注册中心名称")
    url = models.CharField("注册中心地址", max_length=50, help_text="注册中心地址")
    type = models.CharField("注册中心类型", max_length=50, default="eureka", help_text="注册中心类型")
    desc = models.CharField(max_length=255, null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
    deleted_at = models.CharField(max_length=20, null=True)
    deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

    def __repr__(self):
        return '<Register %r>' % self.name

    class Meta:
        db_table = 'register'
        ordering = ('-id',)


class Application(models.Model, ModelMixin):
    name = models.CharField("APP名称", max_length=50, help_text="APP名称", blank=True)
    socket = models.CharField("APP地址", max_length=50, help_text="APP地址", blank=False)
    ipaddr = models.CharField("IP地址", max_length=50, help_text="IP地址", blank=True)
    port = models.CharField("端口", max_length=50, help_text="端口", blank=True)
    register = models.ForeignKey(Register, on_delete=models.CASCADE, verbose_name="APP所属register", help_text="APP所属register")
    status = models.CharField("应用状态", max_length=50, help_text="应用状态", default='UP')
    desc = models.CharField(max_length=255, null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
    deleted_at = models.CharField(max_length=20, null=True)
    deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

    def __repr__(self):
        return '<Application %r-%r>' % (self.name, self.socket)

    class Meta:
        db_table = 'application'
        ordering = ('-id',)
